<template>
  <div>
    <el-card shadow="never" class="mb20">
      <start-title @showTemplate="handleShowTemplate" ref="startTitle" />
    </el-card>
    <el-card shadow="never">
      <start-tabs @refresh="handleRefresh" ref="startTabs" />
    </el-card>
    <template-modal @refresh="handleRefresh" ref="templateModal" />
  </div>
</template>

<script>
import StartTitle from './StartTitle'
import StartTabs from './StartTabs'
import { getSession, setSession, removeSession } from '@/utils/util'
import { taskDetail, taskGetParam } from '@/api/eval'
import TemplateModal from './TemplateModal'

export default {
  components: {
    StartTitle,
    StartTabs,
    TemplateModal
  },
  data() {
    return {
      row: this.$route.params.row || getSession('startTestRow'),
      type: this.$route.params.type || getSession('startTestType'),
      evalDetailedId: null || getSession('evalDetailedId'),
      number: 1
    }
  },
  async created() {
    if (this.$route.params.row) {
      console.log('刚进页面')
      this.number = 1
      setSession('startTestRow', this.$route.params.row)
      setSession('startTestType', this.$route.params.type)
    } else {
      this.number = 2
      console.log('刷新页面')
    }
    console.log('testRow', this.row)
    if (this.row) {
      this.init()
    } else {
      this.$message.error('页面发生错误，即将跳转回列表页')
      setTimeout(() => {
        this.$router.push({
          name: 'evaluateTable'
        })
      }, 1000)
    }
  },
  methods: {
    async init() {
      let res = ''
      const { evalTaskId, planTargetId, evalPlanId } = this.row
      if (this.type === 'evaluate') {
        // 开始考评调用
        if (this.number === 1) {
          res = await taskDetail({ evalTaskId, planTargetId })
        } else {
          // 刷新等情况调用详情，evalDetailedId 需要用开始考评返回的
          res = await taskGetParam({ evalTaskId, evalDetailedId: this.evalDetailedId })
        }
      } else {
        // 详情调用
        console.log('row', this.row)
        const { evalDetailedId } = this.row
        res = await taskGetParam({ evalTaskId, evalDetailedId })
      }
      console.log('考评 res', res)
      let all = []
      // 取到有数据的 evalDetailedId
      res.data.map(v => {
        if (v.evalDetailedItemVOList && v.evalDetailedItemVOList.length > 0) {
          this.evalDetailedId = v.evalDetailedItemVOList[0].evalDetailedId
        }
      })
      const rewardValueSum = res.data[0].rewardValueSum
      setSession('evalDetailedId', this.evalDetailedId)
      res.data.map(v => {
        all = [...all, ...v.evalDetailedItemVOList]
      })
      // 算钱和分数
      let itemScore = null
      let itemMoney = null
      all.map(v => {
        itemScore += Number(v.itemScore)
        itemMoney += Number(v.itemMoney)
      })
      res.data.unshift({
        evalTypeName: '考评分类',
        evalDetailedItemVOList: all
      })
      console.log('res.data', res.data)
      this.number += 1
      this.$refs.startTabs.getData(res.data)
      this.$refs.templateModal.postArr(all)
      // const titleDetailId = res.data[0].evalDetailedItemVOList[0].evalDetailedId
      this.$refs.startTitle.show({ itemScore, itemMoney, rewardValueSum, evalTaskId, evalDetailedId: this.evalDetailedId })
    },
    handleRefresh() {
      this.init()
    },
    handleShowTemplate(num) {
      this.$refs.templateModal.edit(num)
    }
  },
  destroyed() {
    // removeSession('evalDetailedId')
  }
}
</script>
